﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO;

namespace Beyoung.HelloReading.WP
{
    public partial class ReadingPage : PhoneApplicationPage
    {
        private BookModel book;
        public ReadingPage()
        {
            this.InitializeComponent();
        }
        private void tbContent_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            int fontsize = (int)this.tbContent.FontSize;
            double fontspace = (double)(this.tbContent.CharacterSpacing * fontsize / 1000);
            int tbwidth = (int)Application.Current.Host.Content.ActualWidth;
            double arg_43_0 = this.tbContent.ActualHeight;
            int lineFontCount = (int)Math.Floor((double)tbwidth / ((double)fontsize + fontspace));
            int lineNum = (int)((Application.Current.Host.Content.ActualHeight - 30.0 - this.tbContent.FontSize) / this.tbContent.LineHeight + 1.0);
            int pagesize = lineFontCount * lineNum;
            this.book.PageSize = pagesize;
            this.book.LineFontCount = lineFontCount;
            this.tbContent.Text=this.book.NextPage();
            this.tbRate.Text=this.book.ReadRate;
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (e.NavigationMode == NavigationMode.New)
            {
                string filename = string.Empty;
                try
                {
                    if (NavigationContext.QueryString.ContainsKey("filename"))
                    {
                        filename = NavigationContext.QueryString["filename"];
                    }
                    this.book = new BookModel(filename);
                    this.tbContent.LineStackingStrategy = LineStackingStrategy.BlockLineHeight;
                    this.tbContent.Text=this.book.FirstPageContent;
                    this.tbRate.Text=this.book.ReadRate;
                }
                catch (FileNotFoundException)
                {
                }
            }
        }
        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            if (this.book != null)
            {
                this.book.Dispose();
            }
        }
        private void btnSub_Click(object sender, RoutedEventArgs e)
        {
            
        }
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            
        }

    }
}